package com.chance.cc.crawler.development.command.job.domain.autohome;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpConfig;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.downloader.proxy.Proxy;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.development.command.job.CrawlerJobCommand;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;

/**
 * @author lt
 * @version 1.0
 * @date 2021-01-20 16:04:24
 * @email okprog@sina.com
 */
public class AutoHomeRealTimeCrawlerSchedulerJob {
    private static final String domain = "autohome";
    private static final String crawler_level = "normal";

    private static Proxy proxy = new Proxy();
    static {
        //代理配置
        //H5168QRFNIU3804D
        //5F6B3610BB719FAA
        proxy.setHost("http-dyn.abuyun.com");
        proxy.setPort(9020);
        proxy.setUsername("H5168QRFNIU3804D");
        proxy.setPassword("5F6B3610BB719FAA");
    }

    public static void main(String[] args) {
        publishAutoHomeCrawlerScheduleJobInfo(domain,"汽车之家新闻实时采集任务","tmp_vm_auto",doArticleCrawler(),5);
    }

    /**
     * 发布任务信息
     * @return
     */
    public static CrawlerJob publishAutoHomeCrawlerScheduleJobInfo(String domain,String note,String kafkaTopic,CrawlerRequestRecord crawlerRequestRecord,int threadNum){

        CrawlerJob autoHomeScheduler = autoHomeScheduler(domain,kafkaTopic,crawlerRequestRecord,threadNum);

        //发布定时任务
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domain);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(autoHomeScheduler));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote(note);
        crawlerScheduleJob.setCrawlerKey(autoHomeScheduler.generateCrawlerKey());
        CrawlerJobCommand crawlerJobCommand = new CrawlerJobCommand("192.168.1.215",9599);
        HttpPage httpPage = crawlerJobCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布任务：" + httpPage.getRawText());
        return autoHomeScheduler;
    }

    public static CrawlerJob autoHomeScheduler(String domain,String kafkaTopic,CrawlerRequestRecord crawlerRequestRecord,int threadNum){
        //关键词 source record
        CrawlerRequestRecord keywordCrawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain,turnPageItem)
                .httpUrl("http://192.168.1.215:9599//v1/meta/"+domain+"/keys")
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();
        String site = crawlerRequestRecord.tagsCreator().bizTags().getCategoryTag().getKVTagStrVal("site");

        return CrawlerJob.builder()
                .triggerInfo(domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-",site,CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(StringUtils.joinWith("-","crawler",domain, site, crawler_level,"queue")))
                .kafkaResultPipeline(null,kafkaTopic,null)
                .crawlerJobThreadNumber(threadNum)
                .requestRecord(crawlerRequestRecord)
                .build();
    }

    /**
     * 初始record
     * @return
     */
    public static CrawlerRequestRecord doArticleCrawler(){
        String site = "article";

        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, CrawlerEnum.CrawlerRequestType.turnPage)
                .domain(domain)
                .recordKey("https://www.autohome.com.cn/channel2/union/list.html")
                .httpUrl("https://www.autohome.com.cn/channel2/union/list.html")
                .releaseTime(System.currentTimeMillis())
                .httpConfig(HttpConfig.me(domain))
                .filter(CrawlerEnum.CrawlerRecordFilter.keyOrDateRange)
                .addFilterInfo(FilterUtils.redisFilterKeyInfo(domain))
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24,null))
                .proxy(proxy)
                .build();

        requestRecord.tagsCreator().bizTags().addDomain(domain);
        requestRecord.tagsCreator().bizTags().addSite(site);
        return requestRecord;
    }
}
